package com.core.sms.service;

import com.alibaba.fastjson.JSONObject;
import com.common.utils.http.HttpUtils;
import com.common.utils.DateUtils;
import com.common.utils.Md5Utils;
import com.core.sms.common.AbstractSms;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @Author: zql
 * @Description:
 * @Data: 9:45 2021/7/13
 */
@Slf4j
public class Ecloud extends AbstractSms {


    private String url;

    private String enterpriseNo;

    private String account;

    private String signKey;



    public Ecloud(String ip, String port, String enterpriseNo, String account, String signKey) {
        this.url = "http://"+ip+":"+port+"/json/submit";
        this.enterpriseNo = enterpriseNo;
        this.account =account;
        this.signKey = signKey;
    }

    @Override
    public boolean senCode(String mobile, String code) {
        String timestamp = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSSSSS, new Date());
        String sign = Md5Utils.toUpperCase32(this.enterpriseNo + this.account + timestamp + this.signKey);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("enterprise_no", this.enterpriseNo);
        jsonObject.put("account", this.account);
        jsonObject.put("phones", mobile);
        jsonObject.put("content", "短信验证码为：" + code);
        jsonObject.put("subcode", "");
        jsonObject.put("sendtime", "");
        jsonObject.put("timestamp", timestamp);
        jsonObject.put("sign", sign);
        String jsonString = jsonObject.toJSONString();
        log.debug("request:{}",jsonString);
        String responseStr = HttpUtils.sendPost(this.url, jsonString);
        log.debug("responseStr:{}",responseStr);
        return true;
    }
}
